package com.cqcvc.controller;


import com.alibaba.fastjson.JSON;
import com.cqcvc.config.Result;
import com.cqcvc.entity.Role;
import com.cqcvc.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *  前端控制器
 * @author hunter
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequiresPermissions("role:save")
    @PostMapping("/save")
    @ApiOperation("添加新角色")
    public String saveRole(@RequestBody Role role){
        int i=roleService.saveRole(role);
        if (i == 1) {
            return JSON.toJSONString(new Result().setCode(200).setMessage("成功保存角色信息！"));
        } else {
            return JSON.toJSONString(new Result().setCode(500).setMessage("保存角色信息失败！"));
        }
    }

    @RequiresPermissions("role:del")
    @GetMapping("/del")
    @ApiOperation("删除角色")
    public String delRole(Integer id){
        int i=roleService.delRole(id);
        if (i > 0) {
            return JSON.toJSONString(new Result().setCode(200).setMessage("成功删除角色信息！"));
        } else {
            return JSON.toJSONString(new Result().setCode(500).setMessage("删除角色信息失败！"));
        }
    }

    @RequiresPermissions("role:batchDel")
    @PostMapping("/batchDel")
    @ApiOperation("批量删除角色")
    public String batchDelRole(@RequestBody List<Integer> ids){
        roleService.batchDel(ids);
        return JSON.toJSONString(new Result().setCode(200).setMessage("批量删除角色信息成功！"));
    }

    //@RequiresPermissions("role:select")
    @GetMapping("/select")
    @ApiOperation(value = "查询所有角色信息")
    public String selectRole(String pageNum, String pageSize){
        Map<String, Object> map =roleService.selectRole(pageNum,pageSize);
        if ((long) map.get("total") == 0) {
            return JSON.toJSONString(new Result().setCode(200).setMessage("没有角色信息！"));
        } else {
            return JSON.toJSONString(new Result().setCode(200).setData(map));
        }
    }
}
